{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1. 简述混合高斯模型的基本原理，以及通过混合高斯模型进行背景建模的基本思想。\n",
    "\n",
    "    1. 帧差法：\n",
    "    \n",
    "        视频流的相邻两针（灰度图像）做差，不变的背景做差为0，变化的前景做差后应该为较大的值，这样就可以将前景和背景区分开；考虑到环境变化、传感器的影响，将做差后小于一个门限值T的像素认为是背景，大于T的为前景；  \n",
    "        帧差法的缺陷：噪声放大，前景中相邻相似的部分被漏掉；\n",
    "        \n",
    "    2. 高斯背景：\n",
    "        \n",
    "        视频流的一个像素的灰度值随时间变化符合高斯分布\n",
    "        \n",
    "        $$ I(x,y) \\sim N(\\mu,\\sigma^2) $$\n",
    "\n",
    "        可以认为如果一个像素的当前值与均值的差大于3${\\sigma}$，这个像素是前景，否则是背景；\n",
    "        \n",
    "        如果像素的灰度值不符合高斯分布，但任意分布函数也都可以看做多个高斯分布的组合，即混合高斯模型；\n",
    "        \n",
    "    3. 混合高斯模型\n",
    "    \n",
    "        任何一个分布都可以看做多个高斯分布的线性加权组合，像素灰度(随时间)的概率密度函数：\n",
    "        $$ p(I) = {\\sum^{Q}_{q=1}} w_q N(I;\\mu_q,\\sigma_q^2) $$\n",
    "        \n",
    "        通过迭代动态计算$w_q$、$\\mu_q$、$\\sigma_q$，并根据以下公式判断背景分布和前景分布\n",
    "        $$ B={arg_b}min( {\\sum^{b}_{q=1}} w_q >T)$$\n",
    "        说明：将分布的权值从大到小排序，取最少的前b个权值，权值和大于T，那么认为这b个权值对应的分布集合B是背景分布，其他分布是噪声或前景\n",
    "        \n",
    "        当前像素值如果符合背景分布集合B中的某个分布，则像素为背景，否则像素为前景；\n",
    "        "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2. 解释光流计算中的恒定亮度假设，进一步简述L-K光流估计方法的基本原理。 \n",
    "    1. 亮度恒定假设\n",
    "    \n",
    "        光流估计过程中，同一个点在前后两帧图像中亮度不变\n",
    "        \n",
    "        一个像素(x,y)从t到t+1时刻，x方向位移$\\Delta x$，y方向位移$\\Delta y$，有：\n",
    "        $$ I(x+{\\Delta}x,y+{\\Delta}y,t+1) = I(x,y,t)+ {\\frac{\\partial I}{\\partial x}}{\\Delta x} + {\\frac{\\partial I}{\\partial y}}{\\Delta y} + {\\frac{\\partial I}{\\partial t}} $$\n",
    "        \n",
    "        因为假设亮度恒定，图像光滑连续，上面式子约等于$I(x,y,t)$\n",
    "        \n",
    "        $$ {\\frac{\\partial I}{\\partial x}}{\\Delta x} + {\\frac{\\partial I}{\\partial y}}{\\Delta y} + {\\frac{\\partial I}{\\partial t}} = 0 $$\n",
    "        \n",
    "        推得：\n",
    "        \n",
    "        $$ I_x{\\Delta x} + I_y{\\Delta y} = -It $$\n",
    "        \n",
    "        也就是说，通过点在x和y方向的亮度变化和点在两帧之间的灰度变化，可以推得$\\Delta x$和$\\Delta y$\n",
    "        \n",
    "    2. L-K光流估计方法\n",
    "        \n",
    "        1. 可以认为考察点的周围的一个小邻域内的点的位移与考察点近似一致；\n",
    "            $$ I_xi u + I_yi v = -I_ti $$\n",
    "            \n",
    "            写成矩阵方式：\n",
    "            $$ \n",
    "            \\left[ \\begin{matrix} I_{x1} & I_{y1} \\\\ I_{x2} & I_{y2} \\\\ \\vdots & \\vdots \\end{matrix} \\right] \n",
    "            \\left[ \\begin{matrix} u \\\\ v \\end{matrix} \\right]\n",
    "            = - \\left[ \\begin{matrix} I_{t1} \\\\ I_{t2} \\\\ \\vdots \\end{matrix} \\right]\n",
    "            $$\n",
    "            即$A\\mathbf{u}=b$  \n",
    "            其中$A=\\left[ \\begin{matrix} I_{x1} & I_{y1} \\\\ I_{x2} & I_{y2} \\\\ \\vdots & \\vdots \\end{matrix} \\right]$,\n",
    "            $\\mathbf{u}=\\left[ \\begin{matrix} u \\\\ v \\end{matrix} \\right]$,\n",
    "            $b=- \\left[ \\begin{matrix} I_{t1} \\\\ I_{t2} \\\\ \\vdots \\end{matrix} \\right]$\n",
    "            \n",
    "            可以归结为最优化问题\n",
    "            $$ min|| A\\mathbf{u} - b || $$\n",
    "            \n",
    "            用最小二乘法求解：\n",
    "            $$ \\mathbf{u} = (A^T A)^{-1} A^T b $$\n",
    "        2. L-K方法在上述$Au=b$中两边乘以各点对应的权值\n",
    "            $$ WA\\mathbf{u} = Wb $$\n",
    "            其中：\n",
    "            $$ W=\\left[ \\begin{matrix} w_1 & 0 & 0 \\\\ 0 & \\ddots & 0 \\\\ 0 & 0 & w_N \\end{matrix} \\right] $$\n",
    "            类似的用最小二乘求解：$ \\mathbf{u} = (A^T W^2 A)^{-1} A^T W^2 b $\n",
    "            \n",
    "            $A^T A$需要可逆，只有近似角点才可以得到好的光流计算结果；\n",
    "            \n",
    "            为了能处理大位移，引入金字塔L-K方法，大位移可以在大尺度维度下得到处理；"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
